package com.beifu.manager.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.beifu.manager.dao.AssetTypeMapper;
import com.beifu.manager.model.AssetIn;
import com.beifu.manager.model.AssetInPlan;
import com.beifu.manager.model.AssetModel;
import com.beifu.manager.model.AssetModelExample;
import com.beifu.manager.model.AssetModelExample.Criteria;
import com.beifu.manager.model.AssetType;
import com.beifu.manager.model.AssetTypeExample;
import com.beifu.manager.service.assetmodelservice.AssetModelService;
import com.mds.manager.utils.ResponseHelp;
import com.utils.PageUtils;

/**
 * 资产型号管理
* Title: AssetModelController
* Description:
* @author    BaiBing
* @date      2018年6月24日
 */
@Controller
public class AssetModelController {
	
	private Logger Log = Logger.getLogger(this.getClass());
	
	@Autowired
	private AssetTypeMapper assetTypeMapper;
	@Autowired
	private AssetModelService assetModelService;
	
	@RequestMapping("/assetmodel/goAssetModelIndex")
	public String goOrganizationIndex(Model model, HttpServletRequest request){
		Log.info(">> 资产型号管理界面");
		//获取到所有的资产类型
		//查询当前所有分类
		AssetTypeExample assetTypeExample = new AssetTypeExample();
		assetTypeExample.createCriteria();
		List<AssetType> assetTypeAll = assetTypeMapper.selectByExample(assetTypeExample);
		
		request.setAttribute("assetTypeAll", assetTypeAll);
		return "/assetmodel/asset_model";
	}
	
	
	/**
	 * 资产型号列表数据
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/assetmodel/getAssetModelPage",produces=MediaType.APPLICATION_JSON_VALUE+";charset=utf-8")
	public String getPageData(Model model,HttpServletRequest request, HttpServletResponse res){
		String page = request.getParameter("pageNumber");
		String assetTypeSelect = request.getParameter("assetTypeSelect");//资产类型ID
		String assetModelName = request.getParameter("assetModelName");//资产型号名称
		
		AssetModelExample example = new AssetModelExample();
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(assetTypeSelect)){
			criteria.andAssetTypeIdEqualTo(Integer.valueOf(assetTypeSelect));
		}
		if(!StringUtils.isEmpty(assetModelName)){
			criteria.andAssetModelNameLike("%"+assetModelName+"%");
		}
		
		
		PageUtils Page = new PageUtils(page);
		Page.setQueryParames(example);
		Page = assetModelService.getAssetModelPage(Page);
		return ResponseHelp.responseText(Page);
	}
	
	
	
	
	@RequestMapping("/assetmodel/goAssetModelAdd")
	public String goAssetInAdd(Model model, HttpServletRequest request){
		Log.info(">> 添加资产型号界面");
		
		//查询当前所有分类
		AssetTypeExample assetTypeExample = new AssetTypeExample();
		assetTypeExample.createCriteria();
		List<AssetType> assetTypeAll = assetTypeMapper.selectByExample(assetTypeExample);
		
		
		request.setAttribute("assetTypeAll", assetTypeAll);
		
		return "/assetmodel/assetmodel_add";
	}
	
	
	
/*	@RequestMapping("/assetin/goAssetInView")
	public String goAssetInView(Model model, HttpServletRequest request){
		Log.info(">> 查看备品入库单详情界面");
		String rowJson = request.getParameter("rowJson");
		request.setAttribute("parent_row", rowJson);
        
		return "/assetin/assetin_view";
	}*/
	
	
	@ResponseBody
	@RequestMapping("/assetmodel/assetModelAdd")
	public String assetInAdd(Model model, HttpServletRequest request){
		Log.info(">> 添加资产型号");
		
		String assetModelName = request.getParameter("assetModelName");
		String assetTypeSelect = request.getParameter("assetTypeSelect");
		
		assetModelService.createAssetModel(assetModelName,assetTypeSelect);
		
		return ResponseHelp.responseText();
	}
	
	@ResponseBody
	@RequestMapping("/assetmodel/getAssetModelByType")
	public List<AssetModel> getAssetModelByType(Model model, HttpServletRequest request){
		String assetTypeID = request.getParameter("assetTypeID");
		if(!StringUtils.isEmpty(assetTypeID)){
			return assetModelService.getAssetModelByAssetType(assetTypeID);
		}else{
			return null;
		}
		
	}
	
	public static void main(String[] args) {
		String str = "{\"assetInPlans\":[{\"assetInId\":19,\"assetTypeId\":1,\"id\":4,\"inCount\":111,\"remark1\":\"\",\"remark2\":\"\",\"remark3\":\"\"},{\"assetInId\":19,\"assetTypeId\":2,\"id\":5,\"inCount\":222,\"remark1\":\"\",\"remark2\":\"\",\"remark3\":\"\"},{\"assetInId\":19,\"assetTypeId\":3,\"id\":6,\"inCount\":333,\"remark1\":\"\",\"remark2\":\"\",\"remark3\":\"\"},{\"assetInId\":19,\"assetTypeId\":1,\"id\":7,\"inCount\":444,\"remark1\":\"\",\"remark2\":\"\",\"remark3\":\"\"}],\"createTime\":{\"date\":18,\"day\":1,\"hours\":9,\"minutes\":49,\"month\":5,\"seconds\":2,\"time\":1529286542000,\"timezoneOffset\":-480,\"year\":118},\"createUserId\":1,\"createUserInfo\":{\"id\":1,\"loginName\":\"admin\",\"loginPassword\":\"a66abb5684c45962d887564f08346e8d\",\"organizeId\":2,\"roleId\":1,\"userName\":\"超级管理员\"},\"destOrganizeId\":2,\"id\":19,\"inTime\":{\"date\":28,\"day\":1,\"hours\":0,\"minutes\":0,\"month\":4,\"seconds\":0,\"time\":1527436800000,\"timezoneOffset\":-480,\"year\":118},\"orderImage\":\"\",\"remark1\":\"123123\",\"remark2\":\"\",\"remark3\":\"\",\"srcOrganizeId\":1,\"status\":0}";
		JSONObject obj = new JSONObject().fromObject(str);
		
		String assetInPlans = obj.getString("assetInPlans");
		
	
		AssetIn jb = (AssetIn)JSONObject.toBean(obj,AssetIn.class);
		System.out.println(jb);
		System.out.println(jb.getCreateUserInfo().getUserName());
		
		JSONArray array = JSONArray.fromObject(assetInPlans);
		// 转换方法2
        List<?> list2 = JSONArray.toList(array, new AssetInPlan(), new JsonConfig());//参数1为要转换的JSONArray数据，参数2为要转换的目标数据，即List盛装的数据
        System.out.println(((AssetInPlan)list2.get(0)).getInCount());
	}
}
